<?php
/**
 * 库存管理
 * Created by PhpStorm.
 * User: g
 * Date: 2018/4/15
 * Time: 10:16
 */

namespace app\index\controller;

use app\index\validate\Valid;
use think\Request;
use think\Db;

class Kcgl extends Common
{
    /**
     * 库存管理
     */
    public function kcgl()
    {   
        $wlmc = $this->request->param('wlmc');
        $wlxl = $this->request->param('wlxl');
        $wlbh = $this->request->param('wlbh');
        $ck = $this->request->param('ck');
        $this->assign([
            'wlmc'=>$wlmc,
            'wlxl'=>$wlxl,
            'wlbh'=>$wlbh,
            'ck'=>$ck,
        ]);
        $where = [];
        if($wlmc){
            $where['wlmc'] = ['like','%'.$wlmc.'%'];
        }
        if($wlxl){
            $where['wlxl'] = $wlxl;
        }
        if($wlbh){
            $where['wlbh'] = ['like','%'.$wlbh.'%'];
        }
        if($ck){
            $where['ck'] = $ck;
        }
        $count = db('yd_kccx')->where($where)->count();
        $data = db('yd_kccx')->where($where)->paginate(10,$count);
        $page = $data->render();
        $data = $data->items();
        $maxpage=ceil($count/10);
        $this->assign([
            'page'=>$page,
            'data'=>$data,
            'maxpage'=>$maxpage,
            'count'=>$count
        ]);
        $cks = db('cg_ck')->select();
        $this->assign('cks',$cks);
        $kcsl = db('yd_kccx')->where($where)->sum('kcsl');
        $tj = db('yd_kccx')->where($where)->sum('tj');
        $zl = db('yd_kccx')->where($where)->sum('zl');
        $this->assign([
            'kcsl'=>$kcsl,
            'tj'=>$tj,
            'zl'=>$zl,
        ]);
        //负责的仓库
        $ckjc = db('cg_ck')->where('ckgly','like','%"'.session('employee_id').'"%')->column('ckjc');

        $this->assign('ckjc',$ckjc);
        return $this->fetch();
    }

    /**
     * 删除库存
     */
    public function sckc(){
        $ids = $this->request->post('ids/a');
        $bool = db('yd_kccx')->where('id','in',$ids)->update(['isdel'=>1]);
        if(false!==$bool){
            echo json_encode(['success'=>true,'msg'=>'ok']);
        }else{
            echo json_encode(['success'=>false,'msg'=>'error']);
        }
    }

    /**
     * 手动入库
     */
    public function manual_storage()
    {
        if ($_POST) {
            $jb = $this->request->post('jb/a');
            $list = $this->request->post('list/a');
            if (count($list) < 1) {
                echo json_encode(['success' => false, 'msg' => '请至少插入一条明细']);
                die;
            }
            $data = [];
            foreach ($jb as $k => $v) {
                $data[$v['name']] = $v['value'];
            }
            unset($data['remove']);
            $data['sn'] = $this->produce_rkqrd_SN();
            $bool = db('cg_rkqrd')->insertGetId($data);
            if (false !== $bool) {
                if ($data['attach'])
                    db('annex')->where('id', 'in', $data['attach'])->update(['target_table' => 'cg_rkqrd', 'target_id' => $bool]);
                $ins = [];
                $i = 1;
                foreach ($list as $k => $v) {

                    parse_str($v,$t);
                    unset($t['wllx']);
                    $t['rkqrd_id'] = $bool;
                    $t['sn'] = $i;
                    $ins[] = $t;
                    $i++;
                }

                db('cg_rkqrd_list')->insertAll($ins);
                foreach ($ins as $k => $v) {
                    $rkd = [];
                    $rkd['ck'] = $data['dyck'];
                    $rkd['wlxl'] = $data['wllx'];
                    if($data['wllx']==3){
                        $rkd['wllb'] = '成品';
                        $rkd['cppl'] = db('pro_category t')->join('pro_series t2','t2.category_id=t.id')->join('pro_sku t3','t3.series_id=t2.id')->join('pro_products t4','t4.sku_id=t3.id')->where('t4.id',$v['title'])->value('t.category_name');
                        $rkd['gg'] = db('pro_products')->where('id',$v['title'])->value('format');
                        $rkd['tj'] = db('pro_products')->where('id',$v['title'])->value('box_volume');
                        $rkd['zl'] = db('pro_products')->where('id',$v['title'])->value('weight');
                    }else{
                        $rkd['wllb'] = db('pro_material_type t')->join('pro_materials t2','t.id = t2.material_type_id')->where('t2.id',$v['title'])->value('t.type_name');
                        $rkd['cppl'] = '';
                        $rkd['gg'] = db('pro_materials')->where('id',$v['title'])->value('format');
                        $rkd['tj'] = db('pro_materials')->where('id',$v['title'])->value('volume');
                        $rkd['zl'] = db('pro_materials')->where('id',$v['title'])->value('weight');
                    }

                    $rkd['wlbh'] = $v['pro_no'];
                    $rkd['wlid'] = $v['title'];
                    $rkd['wlmc'] = $v['title_name'];
                    $rkd['kcsl'] = $v['num'];
                    $rkd['kcdw'] = $v['unit'];
                    $rkd['dj'] = $v['dj'];
                    $cnt = db('yd_kccx')->where(['wlxl'=>$data['wllx'],'wlid'=>$v['title']])->count();
                    if($cnt){
                        db('yd_kccx')->where(['wlxl'=>$data['wllx'],'wlid'=>$v['title']])->setInc('kcsl',$v['num']);
                    }else{
                        db('yd_kccx')->insert($rkd);
                    }
                }

                echo json_encode(['success' => true, 'msg' => 'ok']);
                die;
            } else {
                echo json_encode(['success' => false, 'msg' => 'error']);
                die;
            }

        }
        $sqr = db('employee')->where('id', session('employee_id'))->value('em_name');
        $sqrbm = db('department t')->join('department_employee t2', 't2.department_id=t.id')->where('t2.employee_id', session('employee_id'))->value('de_name');
        $sqrq = date('Y-m-d');
        $qrr = $sqr;
        $qrrbm = $sqrbm;
        $qrrq = $sqrq;
        $ck = db('cg_ck')->select();
        $this->assign([
            'sqr' => $sqr,
            'sqrbm' => $sqrbm,
            'sqrq' => $sqrq,
            'qrr' => $qrr,
            'qrrbm' => $qrrbm,
            'qrrq' => $qrrq,
            'ck' => $ck,
        ]);
        return $this->fetch();
    }

    /**
     * 出库申请单
     * @return float|int|string
     */
    public function produce_cksqd_SN(){
        //生成编号
        $max = db('cg_cksqd')->where('sn','like',date('Ymd').'%')->max('cast(sn as SIGNED INTEGER)'); //编号递增开始位置基础

        if($max){
            $max = (double)$max;
            $insert_sn = $max+1;
        }else{
            $insert_sn = date('Ymd').'0001';
        }
        return $insert_sn;
    }

    /**
     * 出库申请单
     * @return float|int|string
     */
    public function produce_rkqrd_SN(){
        //生成编号
        $max = db('cg_rkqrd')->where('sn','like',date('Ymd').'%')->max('cast(sn as SIGNED INTEGER)'); //编号递增开始位置基础

        if($max){
            $max = (double)$max;
            $insert_sn = $max+1;
        }else{
            $insert_sn = date('Ymd').'0001';
        }
        return $insert_sn;
    }

    /**
     * 出库单
     * @return float|int|string
     */
    public function produce_ckd_SN(){
        //生成编号
        $max = db('cg_ckd')->where('sn','like',date('Ymd').'%')->max('cast(sn as SIGNED INTEGER)'); //编号递增开始位置基础

        if($max){
            $max = (double)$max;
            $insert_sn = $max+1;
        }else{
            $insert_sn = date('Ymd').'0001';
        }
        return $insert_sn;
    }

    /**
     * 手动出库
     */
    public function manual_storage2()
    {
        if ($_POST) {
            $jb = $this->request->post('jb/a');
            $list = $this->request->post('list/a');
            if (count($list) < 1) {
                echo json_encode(['success' => false, 'msg' => '请至少插入一条明细']);
                die;
            }
            $data = [];
            foreach ($jb as $k => $v) {
                $data[$v['name']] = $v['value'];
            }
            unset($data['remove']);
            $data['sn'] = $this->produce_cksqd_SN();
            $bool = db('cg_cksqd')->insertGetId($data);
            if (false !== $bool) {
                if ($data['attach'])
                    db('annex')->where('id', 'in', $data['attach'])->update(['target_table' => 'cg_rkqrd', 'target_id' => $bool]);

                $ins = [];
                $i = 1;
                foreach ($list as $k => $v) {

                    parse_str($v,$t);
                    $t['lx'] = $t['wllx'];
                    unset($t['wllx']);
                    $t['cksqd_id'] = $bool;
                    unset($t['dwtj']);
                    unset($t['dwzl']);
                    $t['sqsl'] = $t['num'];
                    $t['sn'] = $i;
                    $ins[] = $t;
                    $i++;
                }
                db('cg_cksqd_list')->insertAll($ins);

                //出库单
                $data['cksqdbh'] = $data['sn'];
                $data['sn'] = $this->produce_ckd_SN();
                $ckd_id = db('cg_ckd')->insertGetId($data);
                $ins = [];
                foreach ($list as $k => $v) {
                    parse_str($v,$t);
                    unset($t['wllx']);
                    $t['ckd_id'] = $ckd_id;
                    $ins[] = $t;
                }

                db('cg_ckd_list')->insertAll($ins);

                echo json_encode(['success' => true, 'msg' => 'ok']);
                die;
            } else {
                echo json_encode(['success' => false, 'msg' => 'error']);
                die;
            }

        }
        $sqr = db('employee')->where('id', session('employee_id'))->value('em_name');
        $sqrbm = db('department t')->join('department_employee t2', 't2.department_id=t.id')->where('t2.employee_id', session('employee_id'))->value('de_name');
        $sqrq = date('Y-m-d');
        $qrr = $sqr;
        $qrrbm = $sqrbm;
        $qrrq = $sqrq;
        $ck = db('cg_ck')->select();
        $this->assign([
            'sqr' => $sqr,
            'sqrbm' => $sqrbm,
            'sqrq' => $sqrq,
            'qrr' => $qrr,
            'qrrbm' => $qrrbm,
            'qrrq' => $qrrq,
            'ck' => $ck,
        ]);
        return $this->fetch();
    }

    /**
     * 弹出选择产品明细--销售物料
     */
    public function xiaoshou_list()
    {
        $material_no = $this->request->param('material_no');
        $material_name = $this->request->param('material_name');
        $this->assign([
            'material_name' => $material_name,
            'material_no' => $material_no,
        ]);
        $where = [];
        $where['m_type'] = 2;
        if ($material_no) {
            $where['material_no'] = ['like', '%' . $material_no . '%'];
        }
        if ($material_name) {
            $where['material_name'] = ['like', '%' . $material_name . '%'];
        }
        $count = db('pro_materials')->where($where)->count();
        $data = db('pro_materials')->where($where)->paginate(10, $count);
        $page = $data->render();
        $data = $data->items();
        $maxpage=ceil($count/10);
        foreach ($data as $k => $v) {
            $v['material_type_id'] = db('pro_material_type')->where('id', $v['material_type_id'])->value('type_name');
            $data[$k] = $v;
        }
        $this->assign('data', $data);
        $this->assign('page', $page);
        $this->assign('count',$count);
        $this->assign('maxpage',$maxpage);
        return $this->fetch();
    }

    /**
     * 弹出选择产品明细--销售物料
     */
    public function shengchan_list()
    {
        $material_no = $this->request->param('material_no');
        $material_name = $this->request->param('material_name');
        $this->assign([
            'material_name' => $material_name,
            'material_no' => $material_no,
        ]);
        $where = [];
        $where['m_type'] = 1;
        if ($material_no) {
            $where['material_no'] = ['like', '%' . $material_no . '%'];
        }
        if ($material_name) {
            $where['material_name'] = ['like', '%' . $material_name . '%'];
        }
        $count = db('pro_materials')->where($where)->count();
        $data = db('pro_materials')->where($where)->paginate(10, $count);
        $page = $data->render();
        $data = $data->items();
        $maxpage=ceil($count/10);
        foreach ($data as $k => $v) {
            $v['material_type_id'] = db('pro_material_type')->where('id', $v['material_type_id'])->value('type_name');
            $data[$k] = $v;
        }
        $this->assign('data', $data);
        $this->assign('page', $page);
        $this->assign('count',$count);
        $this->assign('maxpage',$maxpage);
        return $this->fetch('xiaoshou_list');
    }

    /**
     * 弹出选择供应商明细
     */
    public function gys_list()
    {
        $type_id = $this->request->param('type_id');
        $no = $this->request->param('no');
        $gys_name = $this->request->param('gys_name');
        $this->assign([
            'no' => $no,
            'gys_name' => $gys_name,
        ]);
        $where = [];
        $where['type_ids'] = ['like', '%"' . $type_id . '"%'];
        if ($no) {
            $where['no'] = ['like', '%' . $no . '%'];
        }
        if ($gys_name) {
            $where['gys_name'] = ['like', '%' . $gys_name . '%'];
        }
        $count = db('gys')->where($where)->count();
        $data = db('gys')->where($where)->paginate(10, $count);
        $page = $data->render();
        $data = $data->items();
        $maxpage = ceil($count/10);
        foreach ($data as $k => $v) {
            $t = json_decode($v['type_ids']);
            $t = db('gys_type')->where('id', 'in', $t)->column('title');
            $t = implode(',', $t);
            $v['type_ids'] = $t;
            $data[$k] = $v;
        }
        $this->assign([
            'page' => $page,
            'data' => $data,
            'maxpage'=>$maxpage,
            'count'=>$count
        ]);
        return $this->fetch();
    }

    /**
     * 产品
     */
    public function product()
    {
        $id = $this->request->param('id');
        $data = db('pro_products')->where('id', $id)->field('id,unit')->find();
        //查询产品标准价格
        $bzj = db('pro_price')->where('pid', $id)->value('factory');
        $data['lingshou_price'] = $bzj;
        echo json_encode($data);
    }

    /**
     * 出库管理
     */
    public function ckgl()
    {
        $sn = $this->request->param('sn');
        $order_sn = $this->request->param('order_sn');
        $ckdx_name = $this->request->param('ckdx_name');
        $wllx = $this->request->param('wllx');
        $ckfs = $this->request->param('ckfs');
        $start = $this->request->param('start');
        $end = $this->request->param('end');
        $qrck = $this->request->param('qrck');
        $this->assign([
            'sn'=>$sn,
            'order_sn'=>$order_sn,
            'ckdx_name'=>$ckdx_name,
            'wllx'=>$wllx,
            'ckfs'=>$ckfs,
            'start'=>$start,
            'end'=>$end,
            'qrck'=>$qrck,
        ]);
        $where = [];
        if($qrck!==null)
            $where['qrck'] = $qrck;
        if($sn){
            $where['sn'] = ['like','%'.trim($sn).'%'];
        }
        if($order_sn){
            $where['order_sn'] = ['like','%'.trim($order_sn).'%'];
        }
        if($ckdx_name){
            $where['ckdx_name'] = ['like','%'.trim($ckdx_name).'%'];
        }
        if($wllx){
            $where['wllx'] = $wllx;
        }
        if($ckfs){
            $where['ckfs'] = $ckfs;
        }
        if($start&&$end){
            $where['sqrq'] = [['>',$start],['<',$end],'and'];
        }elseif($start&&!$end){
            $where['sqrq'] = ['>',$start];
        }elseif(!$start&&$end){
            $where['sqrq'] = ['<',$end];
        }
        $count = db('cg_cksqd')->where($where)->count();
        $data = db('cg_cksqd')->where($where)->order('id desc')->paginate(10,$count);
//        echo db('cg_cksqd')->getLastSql();
        $page = $data->render();
        $data = $data->items();
        $maxpage = ceil($count/10);
        foreach ($data as $k=>$v){
            $v['sqsl'] = db('cg_cksqd_list')->where('cksqd_id',$v['id'])->sum('sqsl');
            $v['zje'] = db('cg_cksqd_list')->where('cksqd_id',$v['id'])->sum('zje');
            $data[$k] = $v;
        }
        $this->assign([
            'page'=>$page,
            'data'=>$data,
            'maxpage'=>$maxpage,
            'count'=>$count
        ]);
        return $this->fetch();
    }
    /**
     * 确认出库
     */
    public function sell_sure()
    {
        $id = $this->request->param('id');
        if($_POST){
            //出库完毕
            $bfck = $this->request->post('bfck');
            if($bfck==1){ //部分出库

                $ckwb = $this->request->post('ckwb/a');
                $ckyj = $this->request->post('ckyj');
                $dyck = $this->request->post('dyck');
                $qrck = $this->request->post('qrck');
                $tj = $this->request->post('tj/a');
                $zl = $this->request->post('zl/a');
                $num = $this->request->post('num/a');
                if(!$dyck){
                    echo json_encode(['success'=>false,'msg'=>'请选择对应仓库']);die;
                }
                if(in_array('',$num)){
                    echo json_encode(['success'=>false,'msg'=>'请输入出库数量']);die;
                }
                $sqsls = db('cg_cksqd_list')->where(['cksqd_id'=>$id,'qrck'=>0])->field('id,num')->select();
                foreach ($sqsls as $k=>$v){
                    if($num[$v['id']]>$v['num']){
                        echo json_encode(['success'=>false,'msg'=>'出库数量不能大于申请数量']);die;
                    }
                }

                //验证对应仓库的物料数量是否足量
                foreach($num as $k=>$v){
                    $r = db('cg_cksqd_list')->where('id',$k)->field('title,title_name,lx')->find();

                    $kcsl = db('yd_kccx')->where(['wlid'=>$r['title'],'wlxl'=>$r['lx'],'ck'=>$dyck])->value('kcsl');
//                    echo db('yd_kccx')->getLastSql();
                    $kcsl = $kcsl?$kcsl:0;
                    if($v>$kcsl){
                        echo json_encode(['success'=>false,'msg'=>$r['title_name'].'在仓库'.db('cg_ck')->where('id',$dyck)->value('ckjc').'的库存不足（还剩'.$kcsl.db('yd_kccx')->where(['wlid'=>$r['title'],'wlxl'=>$r['lx'],'ck'=>$dyck])->value('kcdw').'）,无法出库']);die;
                    }
                }

                $data = [];
                $data['sn'] = $this->produce_ckd_SN();
                $data['cksqdbh'] = db('cg_cksqd')->where('id',$id)->value('sn');
                $data['sqr'] = db('cg_cksqd')->where('id',$id)->value('sqr');
                $data['sqrmc'] = db('cg_cksqd')->where('id',$id)->value('sqrmc');
                $data['sqrbm'] = db('cg_cksqd')->where('id',$id)->value('sqrbm');
                $data['sqrq'] = db('cg_cksqd')->where('id',$id)->value('sqrq');
                $data['qrr'] = session('employee_id');
                $data['qrrmc'] = db('employee')->where('id',session('employee_id'))->value('em_name');
                $data['qrrbm'] = db('department t')->join('department_employee t2','t.id=t2.department_id')->where('t2.employee_id',session('employee_id'))->value('de_name');
                $data['qrrq'] = date('Y-m-d');
                $data['ckfs'] = db('cg_cksqd')->where('id',$id)->value('ckfs');
                $data['dyck'] = $dyck;
                $data['wllx'] = db('cg_cksqd')->where('id',$id)->value('wllx');
                $data['ckyj'] = $ckyj;
                $data['sfxyfh'] = db('cg_cksqd')->where('id',$id)->value('sfxyfh');
                $data['shdz'] = db('cg_cksqd')->where('id',$id)->value('shdz');
                $data['shdz'] = db('cg_cksqd')->where('id',$id)->value('shdz');
                $data['shlxr'] = db('cg_cksqd')->where('id',$id)->value('shlxr');
                $data['shdh'] = db('cg_cksqd')->where('id',$id)->value('shdh');
                $data['bz'] = db('cg_cksqd')->where('id',$id)->value('bz');
                $data['ckdxlx'] = db('cg_cksqd')->where('id',$id)->value('ckdxlx');
                $data['ckdx'] = db('cg_cksqd')->where('id',$id)->value('ckdx');
                $data['ckdx_name'] = db('cg_cksqd')->where('id',$id)->value('ckdx_name');
                $data['ckyy'] = db('cg_cksqd')->where('id',$id)->value('ckyy');
                $data['attach'] = db('cg_cksqd')->where('id',$id)->value('attach');
                $data['qrck'] = 0;
                $bool = db('cg_ckd')->insertGetId($data);
                $ins = [];
                $i = 1;
                foreach($num as $k=>$v){
                    $t = [];
                    $t['ckd_id'] = $bool;
                    $t['num'] = $v;
                    $t['sqsl'] = db('cg_cksqd_list')->where('id',$k)->value('sqsl');
                    $t['lx'] = $data['wllx'];
                    $t['sn'] = $i;
                    $t['title'] = db('cg_cksqd_list')->where('id',$k)->value('title');
                    $t['title_name'] = db('cg_cksqd_list')->where('id',$k)->value('title_name');
                    $t['unit'] = db('cg_cksqd_list')->where('id',$k)->value('unit');
                    $t['tj'] = db('cg_cksqd_list')->where('id',$k)->value('tj');
                    $t['zl'] =db('cg_cksqd_list')->where('id',$k)->value('zl');
                    $t['dj'] = db('cg_cksqd_list')->where('id',$k)->value('dj');
                    $t['zje'] = round($t['dj']*$v,2);
                    $t['pro_no'] = db('cg_cksqd_list')->where('id',$k)->value('pro_no');
                    $t['bz'] = db('cg_cksqd_list')->where('id',$k)->value('bz');
                    $t['cksqd_id'] = $k;
                    $ins[] = $t;
                    $i++;
                }
                db('cg_ckd_list')->insertAll($ins);

                //修改原申请单明细和申请单总表
                if($ckwb){
                    foreach($ckwb as $k=>$v){
                        db('cg_cksqd_list')->where('id',$k)->update(['qrck'=>$ckwb,'num'=>$num[$k]]);
                    }
                }

                //减掉已经出库的数量
                $t = true;
                foreach($num as $k=>$v){
                    $sqsl = db('cg_cksqd_list')->where('id',$k)->value('num');
                    db('cg_cksqd_list')->where('id',$k)->setDec('num',$v);
                    if($sqsl==$v){
                        db('cg_cksqd_list')->where('id',$k)->update(['qrck'=>1]);
                    }else{
                        $t = false;
                    }
                }
                if($t==true){ //说明全部出库
                    db('cg_cksqd')->where('id',$id)->update(['qrck'=>1]);
                }

                if($qrck){ //全部出库完毕
                    $sybckyy = $this->request->post('sybckyy');
                    $qtyy = $this->request->post('qtyy');
                    if($sybckyy==2){
                        db('cg_cksqd')->where('id',$id)->update(['qrck'=>1,'sybckyy'=>$sybckyy,'qtyy'=>$qtyy]);
                    }else{
                        db('cg_cksqd')->where('id',$id)->update(['qrck'=>1,'sybckyy'=>$sybckyy]);
                    }

                    db('cg_cksqd_list')->where('cksqd_id',$id)->update(['qrck'=>1]);

                }

                //对应仓库出库，仓库里物料数量减量
                foreach($num as $k=>$v){
                    $r = db('cg_cksqd_list')->where('id',$k)->field('title,lx')->find();
                    db('yd_kccx')->where(['wlid'=>$r['title'],'wlxl'=>$r['lx'],'ck'=>$dyck])->setDec('kcsl',$v);
                }

                //修改确认人
                db('cg_cksqd')->where('id',$id)->update([
                    'qrr'=>session('employee_id'),
                    'qrrmc'=>db('employee')->where(['id'=>session('employee_id')])->value('em_name'),
                    'qrrbm'=>db('department t')->join('department_employee t2','t.id=t2.department_id')->where(['t2.employee_id'=>session('employee_id')])->value('de_name'),
                    'qrrq'=>date('Y-m-d')
                ]);

                echo json_encode(['success'=>true,'msg'=>'出库成功']);
            }elseif ($bfck==2){ //出库完毕
                $dyck = $this->request->post('dyck');
                $ckyj = $this->request->post('ckyj');

                //验证对应仓库的物料数量是否足量
                $mx = db('cg_cksqd_list')->where('cksqd_id',$id)->select();
                foreach($mx as $k=>$v){
                    $v['num'];
                    $kcsl = db('yd_kccx')->where(['wlid'=>$v['title'],'wlxl'=>$v['lx'],'ck'=>$dyck])->value('kcsl');

                    $kcsl = $kcsl?$kcsl:0;
                    if($v['num']>$kcsl){
                        echo json_encode(['success'=>false,'msg'=>$v['title_name'].'在仓库'.db('cg_ck')->where('id',$dyck)->value('ckjc').'的库存不足（还剩'.$kcsl.db('yd_kccx')->where(['wlid'=>$v['pro_no'],'wlxl'=>$v['lx'],'ck'=>$dyck])->value('kcdw').'）,无法出库']);die;
                    }
                }
                $data = [];
                $data['sn'] = $this->produce_ckd_SN();
                $data['cksqdbh'] = db('cg_cksqd')->where('id',$id)->value('sn');
                $data['sqr'] = db('cg_cksqd')->where('id',$id)->value('sqr');
                $data['sqrmc'] = db('cg_cksqd')->where('id',$id)->value('sqrmc');
                $data['sqrbm'] = db('cg_cksqd')->where('id',$id)->value('sqrbm');
                $data['sqrq'] = db('cg_cksqd')->where('id',$id)->value('sqrq');
                $data['qrr'] = session('employee_id');
                $data['qrrmc'] = db('employee')->where('id',session('employee_id'))->value('em_name');
                $data['qrrbm'] = db('department t')->join('department_employee t2','t.id=t2.department_id')->where('t2.employee_id',session('employee_id'))->value('de_name');
                $data['qrrq'] = date('Y-m-d');
                $data['ckfs'] = db('cg_cksqd')->where('id',$id)->value('ckfs');
                $data['dyck'] = $dyck;
                $data['wllx'] = db('cg_cksqd')->where('id',$id)->value('wllx');
                $data['ckyj'] = $ckyj;
                $data['sfxyfh'] = db('cg_cksqd')->where('id',$id)->value('sfxyfh');
                $data['shdz'] = db('cg_cksqd')->where('id',$id)->value('shdz');
                $data['shdz'] = db('cg_cksqd')->where('id',$id)->value('shdz');
                $data['shlxr'] = db('cg_cksqd')->where('id',$id)->value('shlxr');
                $data['shdh'] = db('cg_cksqd')->where('id',$id)->value('shdh');
                $data['bz'] = db('cg_cksqd')->where('id',$id)->value('bz');
                $data['ckdxlx'] = db('cg_cksqd')->where('id',$id)->value('ckdxlx');
                $data['ckdx'] = db('cg_cksqd')->where('id',$id)->value('ckdx');
                $data['ckdx_name'] = db('cg_cksqd')->where('id',$id)->value('ckdx_name');
                $data['ckyy'] = db('cg_cksqd')->where('id',$id)->value('ckyy');
                $data['attach'] = db('cg_cksqd')->where('id',$id)->value('attach');
                $data['qrck'] = 0;
                $bool = db('cg_ckd')->insertGetId($data);
                $mx = db('cg_cksqd_list')->where('cksqd_id',$id)->select();
                $ins = [];
                $i = 1;
                foreach($mx as $k=>$v){
                    $t = [];
                    $t['ckd_id'] = $bool;
                    $t['num'] = $v['num'];
                    $t['sqsl'] = db('cg_cksqd_list')->where('id',$k)->value('sqsl');
                    $t['lx'] = $data['wllx'];
                    $t['sn'] = $i;
                    $t['title'] = $v['title'];
                    $t['title_name'] = $v['title_name'];
                    $t['unit'] = $v['unit'];
                    $t['tj'] = $v['tj'];
                    $t['zl'] = $v['zl'];
                    $t['dj'] = $v['dj'];
                    $t['zje'] = round($t['dj']*$v['num'],2);
                    $t['pro_no'] = $v['pro_no'];
                    $t['pro_no'] = $v['bz'];
                    $t['cksqd_id'] = $k;
                    $ins[] = $t;
                    $i++;
                }
                db('cg_ckd_list')->insertAll($ins);

                //原申请单出库完毕
                db('cg_cksqd')->where('id',$id)->update(['qrck'=>1]);
                db('cg_cksqd_list')->where('id',$id)->update(['qrck'=>1,'num'=>0]);

                //对应仓库出库，仓库数量减量
                $mx = db('cg_cksqd_list')->where('cksqd_id',$id)->select();
                foreach($mx as $k=>$v){
                    db('yd_kccx')->where(['wlid'=>$v['pro_no'],'wlxl'=>$v['lx'],'ck'=>$dyck])->setDec('kcsl',$v['num']);
                }
                echo json_encode(['success'=>true,'msg'=>'出库成功']);
            }
            die;
        }

        $data = db('cg_cksqd')->where('id',$id)->find();
        $data['list'] = db('cg_cksqd_list')->where(['cksqd_id'=>$id,'qrck'=>0])->select();
        $data['sqsl_xj'] = db('cg_cksqd_list')->where(['cksqd_id'=>$id,'qrck'=>0])->sum('sqsl');
        $data['num_xj'] = db('cg_cksqd_list')->where(['cksqd_id'=>$id,'qrck'=>0])->sum('num');
        $data['ycksl_xj'] = $data['sqsl_xj']-db('cg_cksqd_list')->where(['cksqd_id'=>$id,'qrck'=>0])->sum('num');
        $t = db('cg_cksqd_list')->where(['cksqd_id'=>$id,'qrck'=>0])->field('tj,num,zl,sqsl')->select();
        $tj_xj = 0;
        $zl_xj = 0;
        $has = 0;
        foreach($t as $k=>$v){
            $tj_xj+=$v['tj']*$v['num'];
            $zl_xj+=$v['zl']*$v['num'];
            if(($v['sqsl']-$v['num'])>0){
                $has = 1;
            }
        }
        if($has==1){
            $data['yck'] = 1;
        }else{
            $data['yck'] = 0;
        }
        $data['tj_xj'] = $tj_xj;
        $data['zl_xj'] = $zl_xj;

        $this->assign('data',$data);

        $ck = db('cg_ck')->select();
        $this->assign('ck',$ck);

        return $this->fetch();
    }
    /**
     * 出库申请单详情
     */
    public function cksqd_view()
    {
        $id = $this->request->param('id');

        $data = db('cg_cksqd')->where('id',$id)->find();
        $data['list'] = db('cg_cksqd_list')->where('cksqd_id',$id)->select();
        $data['sqsl'] = db('cg_cksqd_list')->where('cksqd_id',$id)->sum('sqsl');
        $data['cksl'] = $data['sqsl']-db('cg_cksqd_list')->where('cksqd_id',$id)->sum('num');
        $data['tj'] = db('cg_cksqd_list')->where('cksqd_id',$id)->sum('tj');
        $data['zl'] = db('cg_cksqd_list')->where('cksqd_id',$id)->sum('zl');
        $data['sqsl_xj'] = db('cg_cksqd_list')->where('cksqd_id',$id)->sum('sqsl');
        $data['tj_xj'] = db('cg_cksqd_list')->where('cksqd_id',$id)->sum('tj');
        $data['zl_xj'] = db('cg_cksqd_list')->where('cksqd_id',$id)->sum('zl');
        $data['ckd_sn'] = db('cg_ckd')->where('cksqdbh',$data['sn'])->column('sn');
        $data['dyck'] = db('cg_ck')->where('id',$data['dyck'])->value('ckjc');
        $has = 0;
        foreach($data['list'] as $k=>$v){
            if($v['sqsl']-$v['num']>0){
                $has = 1;
            }
        }
        if($has==1){
            $data['yck'] = 1;
        }else{
            $data['yck'] = 0;
        }
        $this->assign('data',$data);

        return $this->fetch();
    }

    /**
     * 仓库管理
     */
    public function cangkugl()
    {
        $where = [];
        $count = db('cg_ck')->where($where)->count();
        $data = db('cg_ck')->where($where)->paginate(10, $count);
        $page = $data->render();
        $data = $data->items();
        $maxpage = ceil($count/10);
        foreach ($data as $k => $v) {
            $t = unserialize($v['ckgly']);
            $t = db('employee')->where('id', 'in', $t)->column('em_name');
            $t = implode(',', $t);
            $v['ckgly'] = $t;
            $data[$k] = $v;
        }
        $this->assign('data', $data);
        $this->assign('page', $page);
        $this->assign('maxpage',$maxpage);
        $this->assign('count',$count);
//        dump($data);
        return $this->fetch();
    }

    /**
     * 添加仓库
     */
    public function add_cangku()
    {
        if ($_POST) {
            $data = [];
            $data['ckh'] = $this->request->param('ckh');
            $data['ckjc'] = $this->request->param('ckjc');
            $data['cklxr'] = $this->request->param('cklxr');
            $data['cklxdh'] = $this->request->param('cklxdh');
            $data['ckqc'] = $this->request->param('ckqc');
            $data['ckdz'] = $this->request->param('ckdz');
            $data['ckgly'] = $this->request->param('ckgly/a');
            $data['bz'] = $this->request->param('bz');
            $valid = new Valid([
                'ckh|仓库号' => 'require|unique:cg_ck',
                'ckjc|仓库简称' => 'require',
                'cklxr|仓库联系人' => 'require',
                'ckqc|仓库全称' => 'require',
                'ckdz|仓库地址' => 'require',
            ]);
            if (!$valid->check($data)) {
                echo json_encode(['success' => false, 'msg' => $valid->getError()]);
                die;
            }
            $attach = $this->request->param('attach');
            if ($attach) {
                $data['attach'] = serialize($attach);
            }
            if ($data['ckgly']) {
                $data['ckgly'] = serialize($data['ckgly']);
            }
            $data['cuser'] = db('employee')->where('id', session('employee_id'))->value('em_name');
            $data['ctime'] = date('Y-m-d H:i:s');
            $bool = db('cg_ck')->insertGetId($data);
            if (false !== $bool) {
                db('annex')->where('id', $attach)->update(['target_table' => 'cg_ck', 'target_id' => $bool]);
                echo json_encode(['success' => true, 'msg' => 'ok']);
            } else {
                echo json_encode(['success' => false, 'msg' => 'error']);
            }
            die;
        }
        return $this->fetch();
    }

    /**
     * 修改仓库
     */
    public function update_cangku()
    {
        $id = $this->request->param('id');
        if ($_POST) {
            $data = [];
            $data['id'] = $id;
            $data['ckh'] = $this->request->param('ckh');
            $data['ckjc'] = $this->request->param('ckjc');
            $data['cklxr'] = $this->request->param('cklxr');
            $data['cklxdh'] = $this->request->param('cklxdh');
            $data['ckqc'] = $this->request->param('ckqc');
            $data['ckdz'] = $this->request->param('ckdz');
            $data['ckgly'] = $this->request->param('ckgly/a');
            $data['bz'] = $this->request->param('bz');
            $valid = new Valid([
                'ckh|仓库号' => 'require|unique:cg_ck',
                'ckjc|仓库简称' => 'require',
                'cklxr|仓库联系人' => 'require',
                'ckqc|仓库全称' => 'require',
                'ckdz|仓库地址' => 'require',
            ]);
            if (!$valid->check($data)) {
                echo json_encode(['success' => false, 'msg' => $valid->getError()]);
                die;
            }
            $attach = $this->request->param('attach');

            if ($data['ckgly']) {
                $data['ckgly'] = serialize($data['ckgly']);
            }
            $data['puser'] = db('employee')->where('id', session('employee_id'))->value('em_name');
            $data['ptime'] = date('Y-m-d H:i:s');
            $bool = db('cg_ck')->update($data);
            if (false !== $bool) {
                $this->updateAttach();
                echo json_encode(['success' => true, 'msg' => 'ok']);
            } else {
                echo json_encode(['success' => false, 'msg' => 'error']);
            }
            die;
        }
        $data = db('cg_ck')->where('id', $id)->find();
        $data['ckgly'] = unserialize($data['ckgly']);
        foreach ($data['ckgly'] as $k => $v) {
            $t = $v;
            $v = [];
            $v['id'] = $t;
            $v['title'] = db('employee')->where('id', $t)->value('em_name');
            $data['ckgly'][$k] = $v;
        }
        $this->assign('data', $data);
        return $this->fetch('add_cangku');
    }

    //删除行

    /**
     * 修改附件--新增、修改申请时
     * @return mixed
     */
    private function updateAttach()
    {
        $attach = $this->request->post('attach');
        $id = $this->request->param('id');
        $attach = trim($attach, ',');
        $attachids = $attach;

        //先把旧的附件查出来
        $aids = Db::table('cg_ck')->where('id', $id)->value('attach');
        $old = $aids;
        if ($aids) {
            $aids = unserialize($aids);
        }
        if (!is_array($aids)) {
            $aids = [];
        }

        //删除附件
        $remove = $this->request->post('remove');

        if ($remove) {

            $aids = array_diff($aids, explode(',', $remove));
            db('annex')->where('id', 'in', $remove)->update(['isdel' => 1]);
//                echo Db::table('annex')->getLastSql();
        }

        //新增附件
        if ($attach) {
            $attachids = $attach;
            //新旧合并
            $attach = serialize(array_merge(explode(',', $attach), $aids));
        } else { //非新增
            $attach = serialize($aids);
        }

        $data = [];
        $data['attach'] = $attach;
        db('cg_ck')->where(array('id' => $id))->update($data);
        //新增附件\更新附件表
        if (isset($attachids)) {
            db('annex')->where(array('id' => array('in', $attachids)))->update(array('target_id' => $id, 'target_table' => 'cg_ck'));
        }

    }

    /**
     * 导入仓库
     */
    public function lend_cangku()
    {
        return $this->fetch();
    }

    /**
     * 添加销售物料明细
     */
    public function manual_add_alert()
    {
        if ($_POST) {
            $ins = [];
            $ins['sqr'] = $this->request->post('sqr');
            $ins['sqrbm'] = $this->request->post('sqrbm');
            $ins['sqrq'] = $this->request->post('sqrq');
            $ins['qrr'] = $this->request->post('qrr');
            $ins['qrrbm'] = $this->request->post('qrrbm');
            $ins['qrrq'] = $this->request->post('qrrq');
            $ins['rkfs'] = $this->request->post('rkfs');
            $ins['dyck'] = $this->request->post('dyck');
            $ins['wllx'] = $this->request->post('wllx');
            $ins['rkyy'] = $this->request->post('rkyy');
            $ins['bz'] = $this->request->post('bz');
            $ins['qrrk'] = $this->request->post('qrrk');
            $ins['sn'] = $this->produce_rkqrd_SN();
            $id = $this->request->post('id');
            if (!$id) {
                $id = db('cg_rkqrd')->insertGetId($ins);
            }


            $data = [];
            $data['rkqrd_id'] = $id;
//            $data['wllx'] = $this->request->post('wllx');
            $data['title'] = $this->request->post('title');
            $data['gys'] = $this->request->post('gys');
            $data['unit'] = $this->request->post('unit');
            $data['dj'] = $this->request->post('dj');
            $data['num'] = $this->request->post('num');
            $data['zje'] = $this->request->post('zje');
            $data['bz'] = $this->request->post('bz');
            $valid = new Valid([
                'title|名称' => 'require',
                'gys|供应商' => 'require',
                'dj|单价' => 'require',
                'num|数量' => 'require',
            ]);
            $sn = db('cg_rkqrd_list')->where('rkqrd_id',$id)->max('cast(sn as SIGNED INTEGER)');
            $data['sn'] = $sn+1;
            if (false == $valid->check($data)) {
                echo json_encode(['success' => false, 'msg' => 'error']);
                die;
            }
            $bool = db('cg_rkqrd_list')->insert($data);
            if (false !== $bool) {
                $dd = db('cg_rkqrd_list')->where('rkqrd_id', $id)->select();
                $str = '';

                $xj_num = 0;
                $xj_zje = 0;
                foreach ($dd as $k => $v) {
                    $xj_num += $v['num'];
                    $xj_zje += $v['zje'];
                    if ($ins['wllx'] == 1 || $ins['wllx'] == 2) {
                        $wlbh = db('pro_materials')->where('id', $v['title'])->value('material_no');
                        $wlmc = db('pro_materials')->where('id', $v['title'])->value('material_name');

                    } else {
                        $wlbh = db('pro_products')->where('id', $v['title'])->value('pro_no');
                        $wlmc = db('pro_products')->where('id', $v['title'])->value('pro_name');
                    }
                    $gys = db('gys')->where('id', $v['gys'])->value('gys_name');
                    $xh = $k + 1;
                    $str .= <<<EOF
<tr><td>{$xh}</td>
<td><a href="javascript:;" class="btn btn-info modify_manual" data-id="{$v['id']}">修改</a><br/><a href="javascript:;" class="btn btn-info delThis"  data-id="{$v['id']}">删除</a></td>
<td><a href="javascript:;">{$wlbh}</a></td>
<td class="text-left">{$wlmc}</td>
<td class="text-left">{$gys}</td>
<td>{$v['unit']}</td>
<td class="text-right">{$v['dj']}</td>
<td class="text-right">{$v['num']}</td>
<td class="text-right">{$v['zje']}</td></tr>
EOF;
                }
                $str .= '<tr class="text-right font-bold"><td colspan="7">小计：</td><td>' . $xj_num . '</td><td>' . $xj_zje . '</td></tr>';

                echo json_encode(['success' => true, 'msg' => 'ok', 'str' => $str, 'id' => $id]);
                die;
            } else {
                echo json_encode(['success' => true, 'msg' => 'ok']);
                die;
            }
            die;
        }
        return $this->fetch();
    }

    /**
     * 添加销售物料明细
     */
    public function manual_add_alert2()
    {

        return $this->fetch();
    }

    /**
     * 修改销售物料明细
     */
    public function manual_update_alert()
    {
        $data = Request::instance()->get();

        /*if($_POST){

            $id = $this->request->post('id');

            $data = [];
            $data['id'] = $id;
            $data['title'] = $this->request->post('title');
            $data['gys'] = $this->request->post('gys');
            $data['unit'] = $this->request->post('unit');
            $data['dj'] = $this->request->post('dj');
            $data['num'] = $this->request->post('num');
            $data['zje'] = $this->request->post('zje');
            $data['bz'] = $this->request->post('bz');
            $valid = new Valid([
               'title|名称'=>'require',
               'gys|供应商'=>'require',
               'dj|单价'=>'require',
               'num|数量'=>'require',
            ]);
            if(false == $valid->check($data)){
                echo json_encode(['success'=>false,'msg'=>'error']);die;
            }
            $bool = db('cg_rkqrd_list')->update($data);
            if(false!==$bool){
                $rkqrd_id = db('cg_rkqrd_list')->where('id',$id)->value('rkqrd_id');
                $wllx = db('cg_rkqrd')->where('id',$rkqrd_id)->value('wllx');
                $dd = db('cg_rkqrd_list')->where('rkqrd_id',$rkqrd_id)->select();
                $str = '';

                $xj_num = 0;
                $xj_zje = 0;
                foreach($dd as $k=>$v){
                    $xj_num += $v['num'];
                    $xj_zje += $v['zje'];
                    if($wllx==1||$wllx==2){
                        $wlbh = db('pro_materials')->where('id',$v['title'])->value('material_no');
                        $wlmc = db('pro_materials')->where('id',$v['title'])->value('material_name');

                    }else{
                        $wlbh = db('pro_products')->where('id',$v['title'])->value('pro_no');
                        $wlmc = db('pro_products')->where('id',$v['title'])->value('pro_name');
                    }
                    $gys = db('gys')->where('id',$v['gys'])->value('gys_name');
                    $xh = $k+1;
                    $str .= <<<EOF
<tr><td>{$xh}</td>
<td><a href="javascript:;" class="btn btn-info modify_manual" data-id="{$v['id']}">修改</a><br/><a href="javascript:;" class="btn btn-info delThis" data-id="{$v['id']}">删除</a></td>
<td><a href="javascript:;">{$wlbh}</a></td>
<td class="text-left">{$wlmc}</td>
<td class="text-left">{$gys}</td>
<td>{$v['unit']}</td>
<td class="text-right">{$v['dj']}</td>
<td class="text-right">{$v['num']}</td>
<td class="text-right">{$v['zje']}</td></tr>
EOF;
                }
                $str .= '<tr class="text-right font-bold"><td colspan="7">小计：</td><td>'.$xj_num.'</td><td>'.$xj_zje.'</td></tr>';

                echo json_encode(['success'=>true,'msg'=>'ok','str'=>$str]);die;
            }else{
                echo json_encode(['success'=>true,'msg'=>'ok']);die;
            }
            die;
        }
        $data = db('cg_rkqrd_list')->where('id',$id)->find();
        $wllx = db('cg_rkqrd')->where('id',$data['rkqrd_id'])->value('wllx');
        $this->assign('wllx',$wllx);
        if($wllx==1||$wllx==2)
            $data['title_name'] = db('pro_materials')->where('id',$data['title'])->value('material_name');
        else{
            $data['title_name'] = db('pro_products')->where('id',$data['title'])->value('pro_name');
        }
        $data['gys_name'] = db('gys')->where('id',$data['gys'])->value('gys_name');
        $this->assign('data',$data);*/
        $this->assign('data', $data);
        return $this->fetch();
    }

    /**
     * 修改销售物料明细
     */
    public function manual_update_alert2()
    {
        $data = Request::instance()->get();
        $this->assign('data', $data);
        return $this->fetch();
    }

    /**
     * 入库管理
     */
    public function rkgl()
    {
        $kucun = $this->request->param('kucun');
        $this->assign('kucun',$kucun);

        $sn = $this->request->param('sn');
        $this->assign('sn',$sn);
        $rkfs = $this->request->param('rkfs');
        $this->assign('rkfs',$rkfs);
        $lx = $this->request->param('lx');
        $this->assign('lx',$lx);
        $dyck = $this->request->param('dyck');
        $this->assign('dyck',$dyck);
        $start = $this->request->param('start');
        $this->assign('start',$start);
        $end = $this->request->param('end');
        $this->assign('end',$end);

        $where = [];
        if($kucun==3){

        }elseif($kucun==2){
            $where['qrzt'] = 1;
        }elseif($kucun==1){
            $where['qrzt'] = 0;
        }
        if($sn){
            $where['sn'] = ['like','%'.trim($sn).'%'];
        }
        if($rkfs){
            $where['rkfs'] = $rkfs;
        }
        if($lx){
            $where['lx'] = $lx;
        }
        if($dyck){
            $where['dyck'] = $dyck;
        }
        if($start&&!$end){
            $where['sqsj'] = ['>',$start];
        }elseif(!$start&&$end){
            $where['sqsj'] = ['<',$end];
        }elseif($start&&$end){
            $where['sqsj'] = [['>',$start],['<',$end]];
        }
        $cnt = db('cg_rksqd')->where($where)->count();
        $data = db('cg_rksqd')->where($where)->paginate(10,$cnt);
        $page = $data->render();
        $data = $data->items();
        $maxpage = ceil($cnt/10);
        $this->assign([
            'page'=>$page,
            'data'=>$data,
            'count'=>$cnt,
            'maxpage'=>$maxpage
        ]);
        $ck = db('cg_ck')->select();
        $this->assign('ck',$ck);
        return $this->fetch();
    }

    /**
     * 入库申请单详情
     */
    public function xs_detail()
    {
        $id = $this->request->param('id');
        $data = db('cg_rksqd')->where('id',$id)->find();
        $this->assign('data',$data);

        return $this->fetch();
    }

    /**
     * 查看详情
     */
    public function godown_entried()
    {
        $id = $this->request->param('id');
        $data = db('cg_rksqd')->where('id',$id)->find();
        $data['list'] = db('cg_rksqd_list')->where('rksqd_id',$id)->select();
        $data['sqsl_xj'] = db('cg_rksqd_list')->where('rksqd_id',$id)->sum('sqsl');
        $data['je_xj'] = db('cg_rksqd_list')->where('rksqd_id',$id)->sum('je');
        $data['dyrkdbh'] = db('cg_rkqrd')->where('rksqd_id',$id)->value('sn');
        $this->assign('data',$data);
        return $this->fetch();
    }

    /**
     * 入库确认
     */
    public function godown_entry_sure()
    {
        $id = $this->request->param('id');
        if($_POST){
            $data = [];

            $data['dyck'] = $this->request->post('dyck');
            $data['rkyy'] = $this->request->post('rkyj');
            $qrsl = $this->request->post('qrsl/a');
            if(in_array('',$qrsl)){
                echo json_encode(['success'=>false,'msg'=>'请填写确认数量']);die;
            }
            $data['sqr_id'] = session('employee_id');
            $data['sqr'] = db('employee')->where('id',session('employee_id'))->value('em_name');
            $data['sqrbm'] = db('department t')->join('department_employee t2','t.id=t2.department_id')->where('t2.employee_id',session('employee_id'))->value('de_name');
            $data['sqrq'] = date('Y-m-d');
            $data['rkfs'] = db('cg_rksqd')->where('id',$id)->value('rkfs');
            $data['wllx'] = db('cg_rksqd')->where('id',$id)->value('lx');
            $data['bz'] = db('cg_rksqd')->where('id',$id)->value('bz');
            $data['sn'] = $this->produce_rkqrd_SN();
            $bool = db('cg_rkqrd')->insertGetId($data);
            $ins = [];
            $i = 1;
            foreach($qrsl as $k=>$v){
                $t = [];
                $t['rkqrd_id'] = $bool;
                $t['rksqdmx_id'] = $k;
                $t['title'] = db('cg_rksqd_list')->where('id',$k)->value('wlid');
                $t['title_name'] = db('cg_rksqd_list')->where('id',$k)->value('wlmc');
                $t['pro_no'] = db('cg_rksqd_list')->where('id',$k)->value('wlbh');
                $t['gys'] = db('cg_rksqd_list')->where('id',$k)->value('gys_id');
                $t['gys_name'] = db('cg_rksqd_list')->where('id',$k)->value('gys');
                $t['unit'] = db('cg_rksqd_list')->where('id',$k)->value('unit');
                $t['dj'] = db('cg_rksqd_list')->where('id',$k)->value('dj');
                $t['num'] = $v;
                $t['zje'] = $v*$t['dj'];
                $t['sn'] = $i;
                $ins[] = $t;
                $i++;
            }
            db('cg_rkqrd_list')->insertAll($ins);

            if(false!==$bool){
                db('cg_rksqd')->where('id',$id)->update(['qrzt'=>1]);

                //增加库存
                foreach ($ins as $k => $v) {
                    $rkd = [];
                    $rkd['ck'] = $data['dyck'];
                    $rkd['wlxl'] = $data['wllx'];
                    if($data['wllx']==3){
                        $rkd['wllb'] = '成品';
                        $rkd['cppl'] = db('pro_category t')->join('pro_series t2','t2.category_id=t.id')->join('pro_sku t3','t3.series_id=t2.id')->join('pro_products t4','t4.sku_id=t3.id')->where('t4.id',$v['title'])->value('t.category_name');
                        $rkd['gg'] = db('pro_products')->where('id',$v['title'])->value('format');
                        $rkd['tj'] = db('pro_products')->where('id',$v['title'])->value('box_volume');
                        $rkd['zl'] = db('pro_products')->where('id',$v['title'])->value('weight');
                    }else{
                        $rkd['wllb'] = db('pro_material_type t')->join('pro_materials t2','t.id = t2.material_type_id')->where('t2.id',$v['title'])->value('t.type_name');
                        $rkd['cppl'] = '';
                        $rkd['gg'] = db('pro_materials')->where('id',$v['title'])->value('format');
                        $rkd['tj'] = db('pro_materials')->where('id',$v['title'])->value('volume');
                        $rkd['zl'] = db('pro_materials')->where('id',$v['title'])->value('weight');
                    }

                    $rkd['wlbh'] = $v['pro_no'];
                    $rkd['wlid'] = $v['title'];
                    $rkd['wlmc'] = $v['title_name'];
                    $rkd['kcsl'] = $v['num'];
                    $rkd['kcdw'] = $v['unit'];
                    $rkd['dj'] = $v['dj'];
                    $cnt = db('yd_kccx')->where(['wlid'=>$v['title'],'wlxl'=>$data['wllx'],'ck'=>$data['dyck']])->count();
                    if($cnt){
                        db('yd_kccx')->where(['wlid'=>$v['title'],'wlxl'=>$data['wllx'],'ck'=>$data['dyck']])->setInc('kcsl',$v['num']);
                    }else{
                        db('yd_kccx')->insert($rkd);
                    }
                }

                echo json_encode(['success'=>true,'msg'=>'ok']);
            }else{
                echo json_encode(['success'=>false,'msg'=>'error']);
            }
            die;
        }
        $data = db('cg_rksqd')->where('id',$id)->find();
        $data['list'] = db('cg_rksqd_list')->where('rksqd_id',$id)->select();
        $data['sqsl_xj'] = db('cg_rksqd_list')->where('rksqd_id',$id)->sum('sqsl');
        $data['je_xj'] = db('cg_rksqd_list')->where('rksqd_id',$id)->sum('je');
        $this->assign('data',$data);
        $ck = db('cg_ck')->select();
        $this->assign('ck',$ck);
        return $this->fetch();
    }
}